package cn.wildfire.chat.app.extendsion.ui;

import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;
import com.sourcemuch.im.caiba.R2;

import cn.wildfire.chat.app.extendsion.utils.StatusBarUtil;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.kit.user.UserInfoActivity;

/**
 * Create by HEKE, 2019/8/29 15:12
 * TODO 主界面右上角更多弹出选择窗口
 */
public class VoipPopupWindow implements View.OnClickListener {

    private UserInfoActivity userInfoActivity;
    private OnVoipSelecteListener onVoipSelecteListener;

    private PopupWindow window;
    private View mView;
    private TextView audioCallTxt, videoCallTxt,tvCancel;

    private boolean showing;

    private void initViews() {
        mView = View.inflate(userInfoActivity.getApplicationContext(), R.layout.voip_select_window, null);
        audioCallTxt = mView.findViewById(R.id.audioChat);
        videoCallTxt = mView.findViewById(R.id.videoChat);
        tvCancel = mView.findViewById(R.id.tv_cancel_chat);
        audioCallTxt.setOnClickListener(this);
        videoCallTxt.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        window = new PopupWindow(userInfoActivity);
        window.setContentView(mView);
        window.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        window.setFocusable(true);
        window.setTouchable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(null);
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                showing = false;
                setAlph(1f);
                userInfoActivity.getUserInfoFragment().setDeletButtonVisiable(true);
            }
        });
    }

    public VoipPopupWindow(UserInfoActivity userInfoActivity, OnVoipSelecteListener onVoipSelecteListener) {
        this.userInfoActivity = userInfoActivity;
        this.onVoipSelecteListener = onVoipSelecteListener;
        initViews();
    }

    public boolean isShowing() {
        return showing;
    }

    public void show(View anchorView) {
        if (isShowing())
            return;
        if (anchorView == null) {
            int statusbarHeight = StatusBarUtil.getStatusBarHeight(userInfoActivity);
            int toolbarHeight = TextUtils.dip2px(userInfoActivity, 50);
            window.showAtLocation(mView, Gravity.TOP | Gravity.RIGHT, 16, statusbarHeight + toolbarHeight);
            showing = true;
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                window.showAsDropDown(anchorView, 0, 16, Gravity.BOTTOM);
                showing = true;
            }
        }
    }

    public void showCenter() {
        if (isShowing())
            return;
        setAlph(0.8f);
        userInfoActivity.getUserInfoFragment().setDeletButtonVisiable(false);
        showing = true;
        window.showAtLocation(mView, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 0);
    }

    public void hide() {
        if (!isShowing())
            return;
        showing = false;
        window.dismiss();
        setAlph(1f);
        userInfoActivity.getUserInfoFragment().setDeletButtonVisiable(true);
    }

    @Override
    public void onClick(View view) {
        if (onVoipSelecteListener == null)
            return;
        int id = view.getId();
        if (id == R.id.audioChat) {
            onVoipSelecteListener.call(true);
        } else if (id == R.id.videoChat) {
            onVoipSelecteListener.call(false);
        } else if (id == R.id.tv_cancel_chat) {
            hide();
        }
        hide();
    }

    /**
     * Create by HEKE, 2019/8/30 18:53
     * TODO 音视频通话回调接口
     */
    public interface OnVoipSelecteListener {
        void call(boolean isOnlyAudio);
    }

    private void setAlph(float f) {
        WindowManager.LayoutParams lp = userInfoActivity.getWindow().getAttributes();
        //0.0-1.0
        lp.alpha = f;
        userInfoActivity.getWindow().setAttributes(lp);
    }

}
